import {Injectable} from '@nestjs/common';
import {InjectRepository} from '@nestjs/typeorm';
import {Repository} from 'typeorm';
import {product} from './entity/product.entity';
import {Web3Service} from 'src/web3/web3.service';
import {newproduct} from './dto/newproduct.dto';
import {processinfo} from './dto/processinfo.dto';
import {transport} from './dto/transport.dto';
import {collectinfo} from './dto/clooectinfo.dto';

@Injectable()
export class ProductService {
    constructor(
        @InjectRepository(product)
        private readonly proRepository: Repository<product>,
        private web3: Web3Service
    ) {
    }

    async findone(proAdd: string) {
        const product = await this.proRepository.findOne({where: {address: proAdd}});
        return product;
    }

    //创建新产品
    async createPro(newpro: newproduct) {
        const newAdd = await this.web3.createPro(newpro);
        const info: product = {
            id: undefined,
            productName: newpro.proName,
            description: newpro.description,
            address: newAdd,
            applicant: newpro.applicant,
            ownerAddress: newpro.owner
        }
        const newproduct = this.proRepository.create(info);
        return await this.proRepository.save(newproduct);
    }

    //获得所有产品
    async getAllProduct() {
        const proList = await this.web3.getAllProduct()
        return proList;
    }

    //获得某一user下所有产品
    async getProFromUser(userAdd: string) {
        const userProList = await this.web3.getProFromUser(userAdd);
        return userProList;
    }

    async getProCollect(proAdd: string) {
        const res = await this.web3.getProCollect(proAdd);
        return res;

    }

    async getProBasic(proAdd: string) {
        const res = await this.web3.getProBasic(proAdd);
        return res;
    }

    async getProProcess(proAdd: string) {
        const res = await this.web3.getProProcess(proAdd);
        return res;
    }

    async getProTransport(proAdd: string) {
        const res = await this.web3.getProTransport(proAdd);
        return res;
    }

    async getIfApprove(proAdd: string) {
        const res = await this.web3.getIfApprove(proAdd);
        return res;
    }

    async getIsDeleted(proAdd: string) {
        const res = await this.web3.getIsDeleted(proAdd);
        return res;
    }

    async getSellAddress(proAdd: string) {
        const res = await this.web3.getSellAddress(proAdd);
        return res;
    }

    async getAppMessage(proAdd: string) {
        return await this.web3.getAppMessage(proAdd);
    }

    async applyApprove(proAdd: string, certificate: string, price: string) {
        await this.web3.applyApprove(proAdd, certificate, price);
    }

    async applyAgain(proAdd: string, certificate: string, price: string) {
        await this.web3.applyAgain(proAdd, certificate, price);
    }

    async setIfApprove(proAdd: string, approver: string,
                       isApprove: number, appMessage: string) {
        await this.web3.setIfApprove(proAdd, approver, isApprove, appMessage);
    }

    async pushProcessToPro(proAdd: string, processinfo: processinfo) {
        await this.web3.pushProcessToPro(proAdd, processinfo);
    }

    async pushTransportToPro(proAdd: string, trans: transport) {
        await this.web3.pushTransportToPro(proAdd, trans);
    }

    async setProCollect(proAdd: string, collectinfo: collectinfo) {
        await this.web3.setProCollect(proAdd, collectinfo);
    }

    async setIsDeleted(proAdd: string, isDeleted: boolean) {
        await this.web3.setIsDeleted(proAdd, isDeleted);
    }

    async setSellAddress(proAdd: string, sellAdd: string) {
        await this.web3.setSellAddress(proAdd, sellAdd);
    }

    async getCertificate(proAdd: string) {
        return await this.web3.getCertificate(proAdd)
    }
}
